var api = require("../../api/api.js");
var storageManage = require("../../manage/storageManage.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    order_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  getData: function () {
    wx.showLoading()
    var that = this;
    api.default.api('order_count', {}, function (data) {
      var newData = data.data.result;
      that.setData({
        order_list: newData,
        on_credit_order_count: data.data.on_credit_order_count
      })
      if (newData.length <= 0) {
        var the = [{
          "year": formatTime().year,
          "month": formatTime().month,
          "count": 0,
          "cost": 0
        }]
        that.setData({
          order_list: the
        })
      }
      wx.hideLoading()
      wx.hideNavigationBarLoading()

    }, function (e) {
      console.log(e)
      wx.hideLoading()
      wx.hideNavigationBarLoading()

    });

  },
  toMonthOrderList: utils.throttle(function(e){
    var index = e.currentTarget.dataset.idx;
    var time = this.data.order_list[index].year + '-' + this.data.order_list[index].month ;
    wx.navigateTo({
      url: '../monthOrderList/monthOrderList?time='+time,
    })
  }),
  toCreditList: utils.throttle(function (e) {
    wx.navigateTo({
      url: '../creditList/creditList'
    })
  }),
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  //上拉加载
  onReachBottom: function () {

  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getData();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function formatTime() {
  var date = new Date();
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var boj = {
    year: formatNumber(year),
    month: formatNumber(month)
  }
  return boj

}
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}  